package LeetCode.month02.day51;
import LeetCode.month02.day50.ListNode;

public class No61 {
    public ListNode rotateRight(ListNode head, int k) {
        if(k==0||head==null||head.next==null) return head;
        ListNode curr = head;
        int num = 0;
        while(curr!=null){
            num++;
            curr = curr.next;
        }
        if(k%num==0) return head;
        k = k%num;
        curr = head;
        for(int i=0;i<num-k-1;i++){
            curr = curr.next;
        }
        ListNode newhead = curr.next;
        curr.next = null;
        curr = newhead;
        while (curr.next!=null){
            curr = curr.next;
        }
        curr.next = head;
        return newhead;
    }
}
